//
//  RepairItemListView.m
//  HondaPro_2
//
//  Created by Eric on 13-6-3.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "RepairItemListView.h"
#import "MainMenuDataModel.h"
#import "UserDefinedCellForSubView1ForGetJobSheet.h"

@interface RepairItemListView ()
/** 收费区分列表 */
@property(nonatomic,strong)NSMutableDictionary *chargeDiff;
//创建控件
-(void)createStaticImg;
-(void)createTableView;
-(void)createLbl;

//刷新控件内容
-(void)freshenViewDis;

@end

@implementation RepairItemListView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createStaticImg];
        [self createTableView];
        [self createLbl];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 6+[self.repairItemDic count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UserDefinedCellForSubView1ForGetJobSheet *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForSubView1ForGetJobSheet alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    
    
    NSArray *arrRepairItemList = self.repairItemDic;
    if (row<[arrRepairItemList count])
    {
        NSDictionary *dicRepairItemList = [arrRepairItemList objectAtIndex:row];
        for (int i = 0; i<[cell.arrForAllLabelsKeysOnCell count]; i++)
        {
            NSString *strKey = [cell.arrForAllLabelsKeysOnCell objectAtIndex:i];
            id text = dicRepairItemList[strKey];
            //[strKey isEqualToString:@"STD_LABOUR_HOUR"]||[strKey isEqualToString:@"CLAIM_LABOUR"]
            if ([text isKindOfClass:[NSNumber class]]) {//如果是数字
                text = [text stringValue];
            }else if ([strKey isEqualToString:@"CHARGE_PARTITION_CODE"]){
                text = self.chargeDiff[dicRepairItemList[strKey]];
            }
            
            ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]]).text = text;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    else
    {
        for (int i = 0; i<[cell.arrForAllLabelsKeysOnCell count]; i++)
        {
            ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]]).text = @"";
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (NSString *)transString:(id)obj
{
    if ([obj isKindOfClass:[NSString class]]) {
        return obj;
    }
    
    
    NSNumber *newObj = (NSNumber *)obj;
    return newObj.stringValue;
}

#pragma  mark - 创建控件

-(void)createStaticImg
{
    NSArray *arrForAllKeys = [NSArray arrayWithObjects:
                              @"bottomLbl",
                              @"CHARGE_PARTITION_CODE",
                              @"LABOUR_CODE",
                              @"LABOUR_NAME",
                              @"STD_LABOUR_HOUR",
                              @"LABOUR_PRICE",
                              @"LABOUR_AMOUNT",@"DISCOUNT",nil];
    
    NSDictionary *dicForStaticLabelsRect = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSValue valueWithCGRect:CGRectMake(0, 0, self.frame.size.width, 51)],@"bottomLbl",
                                            [NSValue valueWithCGRect:CGRectMake(0, 0, 70, 50)],@"CHARGE_PARTITION_CODE",
                                            [NSValue valueWithCGRect:CGRectMake(71, 0, 210, 50)],@"LABOUR_CODE",
                                            [NSValue valueWithCGRect:CGRectMake(281, 0, 300, 50)],@"LABOUR_NAME",
                                            [NSValue valueWithCGRect:CGRectMake(581, 0, 100, 50)],@"STD_LABOUR_HOUR",
                                            [NSValue valueWithCGRect:CGRectMake(681, 0, 100, 50)],@"LABOUR_PRICE",
                                            [NSValue valueWithCGRect:CGRectMake(781, 0, 100, 50)],@"LABOUR_AMOUNT",
                                            [NSValue valueWithCGRect:CGRectMake(881, 0, 50, 50)],@"DISCOUNT",nil];
    
    NSDictionary *dicForStaticLabelsText = [NSDictionary dictionaryWithObjectsAndKeys:
                                            @"",@"bottomLbl",
                                            @"收费区分",@"CHARGE_PARTITION_CODE",
                                            @"维修项目代码",@"LABOUR_CODE",
                                            @"维修项目名称",@"LABOUR_NAME",
                                            @"标准工时",@"STD_LABOUR_HOUR",
                                            @"工时单价",@"LABOUR_PRICE",
                                            @"工时费",@"LABOUR_AMOUNT",
                                            @"折扣",@"DISCOUNT",nil];
    
    for (int i = 0; i<[arrForAllKeys count]; i++)
    {
        NSString *strKeys = [arrForAllKeys objectAtIndex:i];
        CGRect rect = (CGRect)[[dicForStaticLabelsRect objectForKey:strKeys] CGRectValue];
        UILabel *lbl = [[UILabel alloc] init];
        lbl.frame = rect;
        if ([strKeys isEqualToString:@"bottomLbl"])
        {
            //以灰色为标题label的背景打底色
            //            lbl.backgroundColor = [UIColor grayColor];
            //将灰色背景色修改成浅灰色
            lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        }
        else
        {
            //以浅灰色为标题label的背景色
            lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            //            lbl.backgroundColor = [UIColor redColor];
        }
        lbl.textColor = [UIColor grayColor];
        lbl.font = [UIFont fontWithName:@"ArialMT" size:16];
        lbl.text = [dicForStaticLabelsText objectForKey:strKeys];
        [self addSubview:lbl];
        
        lbl.numberOfLines = 2;
    }
}

-(void)createTableView
{
    tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, 300)];
    
    tableViewDis.delegate = self;
    tableViewDis.dataSource = self;
    
    tableViewDis.backgroundColor = [UIColor clearColor];
    
    [self addSubview:tableViewDis];
}

-(void)createLbl
{
    WorkChargeTotalLbl = [[UILabel alloc] initWithFrame:CGRectMake(600, 400, 300, 50)];
    WorkChargeTotalLbl.backgroundColor = [UIColor whiteColor];
    WorkChargeTotalLbl.font = [UIFont systemFontOfSize:15];
    WorkChargeTotalLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:WorkChargeTotalLbl];
}

#pragma mark - 刷新控件内容

-(void)freshenViewDis
{
    
    [tableViewDis reloadData];
    
    double total = 0;
    for (NSDictionary *dic in self.repairItemDic) {
        total += [dic[@"LABOUR_AMOUNT"]doubleValue];
    }
    WorkChargeTotalLbl.text = [NSString stringWithFormat:@"工费合计：%.2f",total];
}

//创建一个静态lbl
-(UILabel *)createAnStaticLblByFrame:(CGRect)xingCanRect withTxt:(NSString *)strTxt andBackgroundColor:(UIColor *)bgColor
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:xingCanRect];
    lbl.backgroundColor = bgColor;
    lbl.font = [UIFont systemFontOfSize:13];
    lbl.textColor = [UIColor darkGrayColor];
    lbl.text = strTxt;
    
    if ([strTxt isEqualToString:@"序号"])
    {
        lbl.textAlignment = NSTextAlignmentCenter;
    }
    
//    if (![strTxt isEqualToString:@""])
//    {
//        lbl.backgroundColor = [UIColor redColor];
//    }
    
    return lbl;
}

-(NSMutableDictionary *)chargeDiff{
    if (_chargeDiff == nil) {
        _chargeDiff = [NSMutableDictionary dictionary];
        NSMutableArray *arr = [self readershuDICimform][@"TM_CHARGE_PARTITION"];
        for (NSDictionary *dic in arr) {
            NSString *name = dic[@"NAME"];
            NSString *val = dic[@"CODE"];
            [_chargeDiff setObject:name forKey:val];
        }
    }
    return _chargeDiff;
}

-(NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}

@end
